package com.panda.pojo;

import com.panda.expand.CompositeElementSugar;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

@Getter
@Setter
public class Context {

    private Set<String> globalParams;

    private Map<String, String> fragments;

    //这里是对所有的字符串的替换处理结果,key的部分是字符串的名称,value则是字符串的值
    private Map<String, String> stringMap;

    Map config;

    private Map jsFiles, cssFiles;

    private CompositeElementSugar compositeElementSugar;

    private String text;

    //默认为panda
    private String appName = "panda";

    private String parsingComponent;

    public boolean isComponent(String name) {

        if (parsingComponent != null && parsingComponent.equals(name)) {
            return true;
        }

        return componentNames.contains(name);
    }


    private Set<String> js = new HashSet<>();

    private Set<String> css = new HashSet<>();

    //解析出来的所有的服务
    private Set<Service> services = new HashSet<>();

    //根作用域
    private Element rootElement;

    private Element currentElement;

    private Set<Scope> controllerScopes;

    //全部服务
    private Map<String, Service> serviceMap;

    //解析出的组件
    private Map<String, Component> componentMap;

    public Component pubComponent(String name, Component element) {
        return componentMap.put(name, element);
    }

    public Component getComponent(String name) {
        return componentMap.get(name);
    }


    private Set<String> componentNames;


    //添加一个名称
    public void addComponentName(String name) {

        componentNames.add(name);
    }


    public Context() {

        globalParams =new HashSet<>();

        //碎片不会传递,只在本地有效
        fragments = new HashMap<>();


        rootElement = new Element(this, "__root");

        rootElement.setExpect(Element.BTTmp);

        rootElement.setSubBlocks(new ArrayList<>());
        currentElement = rootElement;
        controllerScopes = new HashSet<>();
        stringMap = new LinkedHashMap<>();
        componentMap = new HashMap<>();
        componentNames = new HashSet<>();
        compositeElementSugar = new CompositeElementSugar();

        this.jsFiles = new HashMap();

        this.cssFiles = new HashMap();

    }

    public Context(Context extend) {

        globalParams =new HashSet<>();

        fragments = new HashMap<>();

        rootElement = new Element(this, "__root");
        rootElement.setSubBlocks(new ArrayList<>());
        currentElement = rootElement;
        stringMap = extend.stringMap;

        controllerScopes = extend.controllerScopes;

        serviceMap = extend.getServiceMap();
        this.componentNames = extend.componentNames;
        this.componentMap = extend.componentMap;

        this.compositeElementSugar = extend.compositeElementSugar;
        this.cssFiles = extend.getCssFiles();
        this.jsFiles = extend.getJsFiles();

        this.config = extend.getConfig();
    }

    //继承自某个作用域
    public Context(Context extend, String text) {
        this(extend);
        this.text = text;

    }


    //从上下文中找到某个元素
    public Element find(String name) {
        return rootElement.find(name);
    }


    //从上下文中找到某个元素
    public Element take(String name) {
        return rootElement.take(name);
    }



    @Override
    public String toString() {
        return text;
    }

    private boolean view=false;

}
